package cn.com.fuhuitong.http

import cn.com.fuhuitong.app.BASE_HTTP_URL
import cn.com.fuhuitong.main.home.entity.HomeResponse
import cn.com.fuhuitong.main.host.entity.ConfigResponse
import cn.com.fuhuitong.main.mine.entity.AddressDefaultResponse
import cn.com.fuhuitong.main.order.entity.AliPayResponse
import cn.com.fuhuitong.main.order.entity.WXPayResponse
import io.reactivex.Observable
import io.reactivex.ObservableTransformer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import retrofit2.http.Field

abstract class BaseAbstractManager<ApiService : BaseApiService>(apiServiceClass: Class<ApiService>) {

    protected val apiService: ApiService =
        HttpServiceFactory.create(apiServiceClass, BASE_HTTP_URL)

    /**
     * 在子线程加载数据
     */
    private fun <Response : BaseResponse> observeOnMain(): ObservableTransformer<Response, Response> {
        return ObservableTransformer { upstream: Observable<Response> ->
            upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
        }
    }

    /**
     * 网络加载数据观察
     */
    protected fun <Response : BaseResponse> subscribe(
        observable: Observable<Response>,
        listener: ResponseSubscribeListener<Response>
    ): Disposable {
        return observable.doOnSubscribe {
            listener.onStart()
        }
            .compose(observeOnMain<Response>())
            .subscribe({
                listener.onSuccess(it)
            }, {
                listener.onFailure(it)
            })
    }


    /**
     * 获取配置信息
     * */
    fun configValue(
        name: Array<String>, listener: SubscribeListener<ConfigResponse>
    ): Disposable {
        val charityIndex: Observable<ConfigResponse> =
            apiService.configValue(name)
        return subscribe(charityIndex, ResponseSubscribeListener(listener))
    }

    /**
     * 获取默认地址
     */
    fun addressDefault(
        token: String,
        listener: SubscribeListener<AddressDefaultResponse>
    ): Disposable {
        val addressDefault: Observable<AddressDefaultResponse> = apiService.addressDefault(token)
        return subscribe(addressDefault, ResponseSubscribeListener(listener))
    }

    /**
     * 商品加入购物车
     */
    fun goodsAddCart(
        token: String, goodsId: String, num: String, specsId: String, userId: String,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val goodsAddCart: Observable<BaseResponse> =
            apiService.goodsAddCart(token, goodsId, num, specsId, userId)
        return subscribe(goodsAddCart, ResponseSubscribeListener(listener))
    }

    /**
     * 添加收藏
     * */
    fun addGoodsCollection(
        token: String, goodsId: String,
        listener: SubscribeListener<BaseResponse>
    ): Disposable {
        val goodsAddCart: Observable<BaseResponse> = apiService.addGoodsCollection(token, goodsId)
        return subscribe(goodsAddCart, ResponseSubscribeListener(listener))
    }

    /**
     * 微信支付
     */
    fun wxPay(
        token: String, orderId: String,
        listener: SubscribeListener<WXPayResponse>
    ): Disposable {
        val wxPay: Observable<WXPayResponse> =
            apiService.wxPay(token, orderId)
        return subscribe(wxPay, ResponseSubscribeListener(listener))
    }

    /**
     * 阿里支付
     */
    fun aliPay(
        token: String, orderId: String,
        listener: SubscribeListener<AliPayResponse>
    ): Disposable {
        val aliPay: Observable<AliPayResponse> =
            apiService.aliPay(token, orderId)
        return subscribe(aliPay, ResponseSubscribeListener(listener))
    }
}